#!/bin/sh
#
# Refer to upstream at https://raw.githubusercontent.com/ethersphere/bee-clef/master/packaging/docker/entrypoint.sh
#
# We particularly just need the sections for initialising masterseed.json, and
# then we will set the passwords for using each ethereum account that we have 
# generated.
#
# Ultimately, instead of clef having to be installed on the docker host to get
# this generation done, however about we...just use the container!
#

DATA="{{ paths.root }}/clef"
SECRET="{{ clef.password }}"
CLEF="/usr/local/bin/clef"
IMAGE="ethersphere/clef:{{ versions.clef }}"

# First initialise the masterseed.json used to store the credentials
#
# WARNING: docker must be run WITHOUT a tty for this to work.
#
sudo docker run -i --entrypoint="" -v $DATA:/app/data $IMAGE $CLEF --configdir "/app/data" --stdio-ui init >/dev/null 2>&1 << EOF
$SECRET
$SECRET
EOF

# There's no need to add new accounts, as these should have already been 
# generated by the python scripts.
{% for account in accounts %}
sudo docker run -i --entrypoint="" -v $DATA:/app/data $IMAGE $CLEF --configdir "/app/data" --keystore /app/data/keystore --stdio-ui setpw {{ account.address|lower }} >/dev/null 2>&1 << EOF
$SECRET
$SECRET
$SECRET
EOF
{% endfor %}

# Make sure that the rules are trusted for automatic signing
#
# WARNING: The sha256sum in the attest is based upon the methodology used in
#          the upstream to generate the correct hash. If the rules.js changes
#          this must also be changed.
sudo docker run -i --entrypoint="" -v $DATA:/app/data $IMAGE $CLEF --configdir "/app/data" --keystore /app/data/keystore --stdio-ui attest 37716e8d6a1107c774aaadbec2770ed2131675f966a834d48ea3b39c7715354b >/dev/null 2>&1 << EOF
$SECRET
EOF
